//
//  AppButtonView.m
//  i5db
//
//  Created by iMuse on 15-5-12.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "AppButtonView.h"
#import "AppCenter.h"

@implementation AppButtonView
{
    UILabel* leftLabel;
    UILabel* rightLabel;
    UILabel* line;
    NSString * leftString;
    NSString * rightString;
}

- (id)init
{
    self = [super init];
    if (self) {
       
        [self initView];
    }
    return self;
}

-(void)updateConstraints
{
    [super updateConstraints];
    [leftLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    [leftLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [leftLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [leftLabel autoSetDimension:ALDimensionWidth toSize:40];
    
    [rightLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:leftLabel];
    [rightLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [rightLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    [rightLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    
    [line autoSetDimension:ALDimensionHeight toSize:0.3];
    [line autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [line autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [line autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    
}
-(void) initView
{
    [self setNeedsUpdateConstraints];
    [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.backgroundColor = [UIColor whiteColor];
    leftLabel = [UILabel newAutoLayoutView];
    [leftLabel sizeToFit];
    leftLabel.adjustsFontSizeToFitWidth = YES;
    leftLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:leftLabel];
    
    rightLabel = [UILabel newAutoLayoutView];
    [rightLabel sizeToFit];
    rightLabel.adjustsFontSizeToFitWidth = YES;
    
    rightLabel.font = [UIFont systemFontOfSize:13];
    rightLabel.textColor = [AppCenter color:@"126,126,126,1"];
    rightLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:rightLabel];
    
    line = [UILabel newAutoLayoutView];
    [self addSubview:line];
    line.backgroundColor = [AppCenter color:@"188,188,188"];
    
}

-(void) setLeftText:(NSString*)_text
{
    leftLabel.text = _text;
    leftString = _text;
}
-(void)setRightText:(NSString*)_text
{
    rightLabel.text = _text;
    rightString = _text;
}
-(void)hiddenLine
{
    line.hidden = YES;
}
-(void)buttonClick:(UIButton*)sender
{
    NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.tag] forKey:@"tag"];
    
    if (!rightString) {
        rightString = @" ";
    }
    
    if (!leftString) {
        leftString = @" ";
    }
    
    [dic setObject:rightString forKey:@"right"];
    [dic setObject:leftString forKey:@"left"];
    
    if ([self.delegate respondsToSelector:@selector(onTapButton:)])
    {
        [self.delegate performSelector:@selector(onTapButton:) withObject:dic];
    }
}











@end
